package com.shuqi.activity.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.common.utils.SqImageManager;
import com.shuqi.controller.R;
import com.shuqi.model.bean.OfferWallInfo;

/**
 * 推广墙顶部适配器
 * 
 * @author linyuren
 * 
 */
public class OfferWallGridAdapter extends BaseAdapter {
    /** from xml to View */
    private LayoutInflater inflater;
    /** 数据集合 */
    private List<OfferWallInfo> data;

    public OfferWallGridAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
    }

    public void setList(List<OfferWallInfo> data) {
        this.data = data;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data == null ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // 获取当前Item
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = inflater.inflate(R.layout.itemlayout_offerwall_grid, parent, false);
        }
        // 保存当前Item中控件的引用, 每个View对象里面可以通过setTag方法保存一个对象
        Holder holder = (Holder) view.getTag();
        if (holder == null) {
            holder = new Holder();
            holder.iv = (ImageView) view.findViewById(R.id.itemlayout_offerwall_grid_iv);
            holder.tv = (TextView) view.findViewById(R.id.itemlayout_offerwall_grid_tv);
            holder.hline = view.findViewById(R.id.itemlayout_offerwall_grid_hline);
            view.setTag(holder);
        }
        // 给当前Item中的控件设值
        holder.tv.setText(data.get(position).getName());

        int lineSize = 0;
        int topSize = 0;
        try {
            lineSize = Integer.parseInt(data.get(position).getLineSize());
            topSize = Integer.parseInt(data.get(position).getTopSize());
        } catch (Exception e) {
        }
        if (lineSize > 0) {
            if (position > (topSize - (topSize % lineSize))) {
                holder.hline.setVisibility(View.GONE);
            } else {
                holder.hline.setVisibility(View.VISIBLE);
            }
        }

        String imageUrl = data.get(position).getIconUrl();
        SqImageManager.getImageManager().displayImage(imageUrl, holder.iv,
                SqImageManager.IMG_DEFAULT);
        return view;
    }

    private static class Holder {
        public ImageView iv;
        public TextView tv;
        public View hline;// 横线
    }
}
